using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CamberSlider : MaskableGraphic
{
    private List<UIVertex> verticeList = new List<UIVertex>();

    public float maxFill = 360;

    [Range(0, 360)]
    public float fillRange;         
    public Texture texture;         

    public float radius;

    [Range(3, 360)]
    public int segment;

    public float lineWidth;     

    public override Texture mainTexture
    {
        get
        {
            return texture;
        }
    }

    protected override void OnPopulateMesh(VertexHelper vh)
    {
        vh.Clear();            

        if(verticeList.Count > 2 ) 
        {
            lineWidth = Mathf.Clamp(lineWidth, 1, radius);
            UIVertex uiVertex0;
            UIVertex uiVertex1;
            UIVertex uiVertex2;
            UIVertex uiVertex3;
            Vector3 pointToCenter;

            for (int i = 1; i < verticeList.Count; i++) 
            {
                UIVertex[] uiVertexs = new UIVertex[4];
                
                uiVertex0 = new UIVertex();
                uiVertex0.position = verticeList[i].position;
                uiVertex0.color = color;
                if(texture != null)
                {
                    uiVertex1.uv0 = new Vector2(uiVertex0.position.x / texture.width + 0.5f, uiVertex0.position.y / texture.height + 0.5f);
                }

                uiVertex1 = new UIVertex();
                pointToCenter = (verticeList[i].position - Vector3.zero).normalized;
                uiVertex1.position = verticeList[i].position - pointToCenter * lineWidth;
                uiVertex1.color = color;
                if (texture != null)
                {
                    uiVertex1.uv0 = new Vector2(uiVertex1.position.x / texture.width + 0.5f, uiVertex1.position.y / texture.height + 0.5f);
                }


                uiVertex2 = new UIVertex();
                uiVertex2.position = verticeList[i - 1].position;
                uiVertex2.color = color;
                if (texture != null)
                {
                    uiVertex2.uv0 = new Vector2(uiVertex2.position.x / texture.width + 0.5f, uiVertex2.position.y / texture.height + 0.5f);
                }

                uiVertex3 = new UIVertex();
                pointToCenter = (verticeList[i - 1].position - Vector3.zero).normalized;
                uiVertex3.position = verticeList[i - 1].position - pointToCenter * lineWidth;
                uiVertex3.color = color;
                if (texture != null)
                {
                    uiVertex3.uv0 = new Vector2(uiVertex3.position.x / texture.width + 0.5f, uiVertex3.position.y / texture.height + 0.5f);
                }

                uiVertexs[3] = uiVertex0;
                uiVertexs[2] = uiVertex1;
                uiVertexs[0] = uiVertex2;
                uiVertexs[1] = uiVertex3;
                vh.AddUIVertexQuad(uiVertexs);
            }
        }
    }

    private void Update()
    {
        if (/*Time.frameCount % 5 == 0 &&*/ fillRange <= maxFill)        
        {
            verticeList.Clear();
            
            float spanAngle = 360 / (segment * 1.0f);
            
            for (int i = 0; i < segment + 1; i++)               
            {
                float angle = Mathf.Clamp(i * spanAngle, 0, fillRange);
            
                float x = radius * Mathf.Cos(angle * Mathf.Deg2Rad);
            
                float y = radius * Mathf.Sin(angle * Mathf.Deg2Rad);
            
                UIVertex uiVertex = new UIVertex();
                uiVertex.position = new Vector3(x, y, 0);
            
                verticeList.Add(uiVertex);
            }
            
            SetVerticesDirty();
        }

    }
}
